﻿using System.Collections.Generic;
using Models;
using Providers;
using System;

namespace Services
{
    /// <summary>
    /// Manage Servers
    /// </summary>
    public class ServersService
        : ServiceBase<SQPlugin.ISQPlugin>
    {
        /// <summary>
        /// Initialise the Servers service
        /// </summary>
        public ServersService(Guid serverID)
        {
            ServerID = serverID;
            Initialise();
        }

        /// <summary>
        /// The server ID that is currently associated with this service. This maps down to the PlugIn ID that we will be
        /// using
        /// </summary>
        public Guid ServerID
        {
            get;
            private set;
        }

        /// <summary>
        /// Initialise and return the Edit Database user control for the server associated with this service
        /// </summary>
        /// <returns>A user control that impliments IEditDatabase</returns>
        public SQPlugin.IEditDatabase GetEditDatabaseUI()
        {
            return Provider.GetEditDatabaseUI();
        }

        /// <summary>
        /// Initialise and return the Server Details user control for the server associated with this service
        /// </summary>
        /// <returns>A user control that impliments IServerDetails</returns>
        public SQPlugin.IServerDetails GetServerDetailsUI()
        {
            return Provider.GetServerDetailsUI();
        }

        /// <summary>
        /// Get a list of databases on the specified server
        /// </summary>
        /// <param name="server">The server that we want to retrieve databases from </param>
        /// <param name="errorMsg">If an error occured the message will returned here, otherwise blank</param>
        /// <returns>A list of databases loaded from the specified server</returns>
        public List<Database> GetDatabases(Server server, out string errorMsg)
        {
            return Provider.GetDatabases(server, out errorMsg);
        }

        /// <summary>
        /// Get the results of a query based on the supplied database and query
        /// </summary>
        /// <param name="database">The database to query</param>
        /// <param name="query">The query to run</param>
        /// <returns>A query result</returns>
        public QueryResult RunQuery(Database database, Models.Query query)
        {
            return Provider.RunQuery(database, query);
        }

        /// <summary>
        /// Check to see if the supplied database model can actually connect the database it is set up to connect to
        /// </summary>
        /// <param name="database">The database to check</param>
        /// <param name="errorMsg">If an error occured the message will returned here, otherwise blank</param>
        /// <returns>True if the database connects without any errors</returns>
        public bool ValidateDatabase(Database database, out string errorMsg)
        {
            return Provider.ValidateDatabase(database, out errorMsg);
        }

        /// <summary>
        /// Validate the server object to see if all the correct connection details are provided
        /// </summary>
        /// <param name="server">The server object to validate</param>
        /// <returns>True if the server object contains valid connection details</returns>
        public bool ValidateServer(Server server)
        {
            return Provider.ValidateServer(server);
        }

        /// <summary>
        /// Return a dictionary of loaded server plug ins
        /// </summary>
        /// <returns>A dictionary of loaded server plugins keyed by their id</returns>
        public static Dictionary<Guid, string> GetServerPluginList()
        {
            return ProviderFactory.GetPlugInList();
        }

        /// <summary>
        /// Create a provider of the type used by the service
        /// </summary>
        /// <returns>A provider</returns>
        protected override SQPlugin.ISQPlugin CreateProvider()
        {
            return ProviderFactory.CreateServerProvider(ServerID);
        }
    }
}
